﻿using Engine.Graphics.VertexStructures;
using SharpDX.Direct3D11;

namespace Engine.Graphics
{
    public class StandardGraphicBuffers
    {
        #region Fields (4)

        #region Public Fields

        #region Public Fields

        public bool Disposed;

        #endregion Public Fields

        #endregion Public Fields

        #region Private Fields

        #region Private Fields

        private Device device;
        private SharpDX.Direct3D11.Buffer indexBuffer;
        private BufferDescription indexBufferDescription;
        private SharpDX.Direct3D11.Buffer vertexBuffer;
        private BufferDescription vertexBufferDescription;

        #endregion Private Fields

        #endregion Private Fields

        #endregion Fields

        #region Constructors (1)

        #region Public Constructors

        #region Public Constructors

        public StandardGraphicBuffers(Device device)
        {
            this.device = device;
            vertexBufferDescription = new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = 0,
                // OptionFlags = ResourceOptionFlags.BufferStructured,
                SizeInBytes = 0,
                Usage = ResourceUsage.Default
            };
            indexBufferDescription = new BufferDescription()
            {
                BindFlags = BindFlags.IndexBuffer,
                CpuAccessFlags = 0,
                SizeInBytes = 0,
                Usage = ResourceUsage.Default,
                OptionFlags = ResourceOptionFlags.None
            };
        }

        #endregion Public Constructors

        #endregion Public Constructors

        #endregion Constructors

        #region Properties (2)

        #region Public Properties

        #region Public Properties

        public SharpDX.Direct3D11.Buffer IndexBuffer
        {
            get { return indexBuffer; }
            set { indexBuffer = value; }
        }

        public SharpDX.Direct3D11.Buffer VertexBuffer
        {
            get { return vertexBuffer; }
            set { vertexBuffer = value; }
        }

        #endregion Public Properties

        #endregion Public Properties

        #endregion Properties

        #region Methods (3)

        // Public Methods (1) 

        #region Public Methods

        #region Public Methods

        public void UpdateVertexBuffer(MyOwnVertex.MyPositionNormalTextured[] vertices)
        {
            if (vertices.Length == 0)
                return;

            int size;
            if (this.vertexBuffer != null && ((size = MyOwnVertex.MyPositionNormalTextured.SizeInBytes()) * vertices.Length) != this.vertexBuffer.Description.SizeInBytes)
            {
                this.vertexBuffer.Dispose();
                this.vertexBuffer = null;
            }
            this.vertexBufferDescription.SizeInBytes = MyOwnVertex.MyPositionNormalTextured.SizeInBytes() * vertices.Length;
            if (this.vertexBuffer == null)
            {     //this.vertexBuffer = new Buffer(this.device, MyOwnVertex.MyPositionNormalTextured.SizeInBytes() * vertices.Length, ResourceUsage.Default, MyOwnVertex.MyPositionNormalTextured.Format, Graphic.DefaultPool);
                // var dataBuffer =  DataStream.Create(vertices,false,false,0,true);
                this.vertexBuffer = Buffer.Create(this.device, vertices, vertexBufferDescription);
            }
        }

        #endregion Public Methods

        #endregion Public Methods

        // Internal Methods (2) 

        #region Internal Methods

        #region Internal Methods

        internal void Dispose()
        {
            if (this.indexBuffer != null)
            {
                this.indexBuffer.Dispose();
                this.indexBuffer = null;
            }
            if (this.vertexBuffer != null)
            {
                this.vertexBuffer.Dispose();
                this.vertexBuffer = null;
            }
        }

        internal void UpdateIndexBuffer(ushort[] indices)
        {
            if (indices.Length <= 0)
                return;

            if (this.indexBuffer != null)
            {
                // if (this.indexBuffer.Description != null) bei slimdx ging das noch
                if (this.indexBuffer.Description.SizeInBytes != indices.Length * sizeof(short))
                {
                    this.indexBuffer.Dispose();
                    this.indexBuffer = null;
                }
            }

            this.indexBufferDescription.SizeInBytes = indices.Length * sizeof(short);
            if (this.indexBuffer == null)
                this.indexBuffer = Buffer.Create(this.device, indices, indexBufferDescription);
            //this.indexBuffer = new Buffer(this.device, indices.Length * sizeof(short), Usage.WriteOnly, Graphic.DefaultPool, true);
        }

        #endregion Internal Methods

        #endregion Internal Methods

        #endregion Methods
    }
}